#include "MagicBox.h"
#include "baseapplication.h"

using namespace Ogre;
const float PI = 3.1415927f;

CMagicBox::CMagicBox(void)
{
	mEntity = NULL;
	mNode = NULL;
	mAnimationState = NULL;
	mState = INIT;
}

CMagicBox::~CMagicBox(void)
{
}

void CMagicBox::createFrameListener(Ogre::Root * root)
{
	root->addFrameListener(this);
}

bool CMagicBox::frameStarted(const Ogre::FrameEvent& evt)
{
	if (mAnimationState)
	{
		mAnimationState->addTime(evt.timeSinceLastFrame);
	}

	return true;
}

bool CMagicBox::frameEnded(const FrameEvent& evt)
{
	if (mAnimationState && mState == INIT && mAnimationState->hasEnded())
	{
		mAnimationState->setEnabled(false);
		mAnimationState = mEntity->getAnimationState("move");
		mAnimationState->setLoop(false);
		mAnimationState->setTimePosition(0.0f);
		mAnimationState->setEnabled(true);
		mState = MOVE;
	}
	if (mAnimationState && mState == MOVE && mAnimationState->hasEnded())
	{
		mState = STARTED;
	}
	return true;
}

void CMagicBox::createScene(Ogre::SceneManager* sceneMgr)
{
	mEntity = sceneMgr->createEntity("MagicBox", "BOX.mesh");

	mNode = sceneMgr->getRootSceneNode()->createChildSceneNode("MagicBoxNode", Ogre::Vector3(0.0f, -0.0f, -0.0f));
// 	mNode->rotate(Quaternion(Degree(-160), Vector3::UNIT_Y));

	mNode->attachObject(mEntity);
}

void CMagicBox::Start()
{
	if (mState == INIT)
	{
		mAnimationState = mEntity->getAnimationState("sperate");
		mAnimationState->setLoop(false);
		mAnimationState->setEnabled(true);
	}
}

void CMagicBox::Right()
{
	if (!mAnimationState)
	{
		return;
	}
	mAnimationState->setEnabled(false);
	switch(mState)
	{
	case TVDRAM:
		mAnimationState = mEntity->getAnimationState("tvdram_tv");
		mState = TV;
		break;
	case TV:
		mAnimationState = mEntity->getAnimationState("tv_video");
		mState = VIDEO;
		break;
	case VIDEO:
		mAnimationState = mEntity->getAnimationState("video_setup");
		mState = SETUP;
		break;
	case SETUP:
		mAnimationState = mEntity->getAnimationState("setup_app");
		mState = APP;
		break;
	case APP:
		mAnimationState = mEntity->getAnimationState("app_tvdram");
		mState = TVDRAM;
		break;
	}
	mAnimationState->setLoop(false);
	mAnimationState->setTimePosition(0.0f);
	mAnimationState->setEnabled(true);
}

void CMagicBox::Left()
{
	if (!mAnimationState)
	{
		return;
	}
	mAnimationState->setEnabled(false);
	switch(mState)
	{
	case TVDRAM:
		mAnimationState = mEntity->getAnimationState("tvdram_app");
		mState = APP;
		break;
	case TV:
		mAnimationState = mEntity->getAnimationState("tv_tvdram");
		mState = TVDRAM;
		break;
	case VIDEO:
		mAnimationState = mEntity->getAnimationState("video_tv");
		mState = TV;
		break;
	case SETUP:
		mAnimationState = mEntity->getAnimationState("setup_video");
		mState = VIDEO;
		break;
	case APP:
		mAnimationState = mEntity->getAnimationState("app_setup");
		mState = SETUP;
		break;
	}
	mAnimationState->setLoop(false);
	mAnimationState->setTimePosition(0.0f);
	mAnimationState->setEnabled(true);
}
